﻿using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _10_05Prism导航功能.ViewModels
{
    class MainViewModel: BindableBase
	{
		//
		public readonly IRegionManager regionManager;
		public DelegateCommand<string> OpenCommand { get; private set; }

		//导航日志
		private IRegionNavigationJournal journal;
		public DelegateCommand BackCommand { get; private set; }

		public MainViewModel(IRegionManager regionManager)
		{
			OpenCommand = new DelegateCommand<string>(Open);
			BackCommand = new DelegateCommand(Back);
			this.regionManager = regionManager;
		}


		private void Open(string obj)
		{
			NavigationParameters keys = new NavigationParameters();
			keys.Add("Title", "Hello");

			//通过IRegionManager接口获取当前全局定义可用区域
			//往这个区域动态的去设置内容
			//设置内容的方式是通过依赖注入的形式

			//没有返回
			//regionManager.Regions["ContentRegion"].RequestNavigate(obj,keys);

			//带返回的
			regionManager.Regions["ContentRegion"].RequestNavigate(obj,callBack=> 
			{
				if ((bool)callBack.Result)
				{
					journal = callBack.Context.NavigationService.Journal;
				}
			},
			keys);

		}

		private void Back()
		{
			if(journal.CanGoBack)
			journal.GoBack();
		}

	
	}

}
